package cn.openea.perpartner.meily.behavioral.controller;

import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.behavioral.entity.BehavioralData;
import cn.openea.perpartner.meily.behavioral.entity.BehavioralDataVo;
import cn.openea.perpartner.meily.behavioral.service.IBehavioralDataService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author rains
 * @since 2023-06-20 10:55
 */
@Api(tags = "行为汇集")
@RestController
@RequestMapping("/behavioral")
public class BehavioralDataController extends BaseController {

    @Resource
    private IBehavioralDataService behavioralDataService;

    @ApiOperation("列表")
    @PostMapping("/list")
    public TableDataInfo getListByType(@RequestBody BehavioralDataVo vo) {
        startPage();
        List<?> list = this.behavioralDataService.getListByType(vo);
        return getDataTable(list);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BehavioralData behavioralData) {
        return success(this.behavioralDataService.save(behavioralData));
    }

    @ApiOperation("获取详情")
    @GetMapping("/info/{id}")
    public AjaxResult getInfoById(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return AjaxResult.error("数据异常，id为空");
        }
        return AjaxResult.success("获取详情成功！", this.behavioralDataService.getById(id));
    }

    @ApiOperation("处理历史数据")
    @PostMapping("/deal/historical")
    public AjaxResult dealHistoricalData(@RequestBody JSONObject object){
        if("1".equals(object.getString("runningFlag"))){
            if(!this.behavioralDataService.dealHistoricalData()){
                return AjaxResult.error("处理历史数据失败！");
            }
        } else {
            return AjaxResult.error("执行标志的参数传输错误，不执行！");
        }
        return AjaxResult.success("处理历史数据成功！");
    }
}
